package fire.and.near.challenger.util;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import fire.and.near.challenger.database.ChallengeModel;
import fire.and.near.challenger.database.ChallengeModel.ChallengeType;
import fire.and.near.challenger.database.ChallengeStatus;
import fire.and.near.challenger.database.FriendModel;
import fire.and.near.challenger.database.TableConstants.ChallengesTable;
import fire.and.near.challenger.database.TableConstants.FriendsTable;

public class ChallengeUtil {

	public static String challengeListToJsonString(
			List<ChallengeModel> challenges) {

		JSONArray jsonArr = new JSONArray();

		for (int i = 0; i < challenges.size(); i++) {
			JSONObject json = new JSONObject();
			try {
				json.put(ChallengesTable.ID, challenges.get(i).getID());
				json.put(ChallengesTable.DESCRIPTION, challenges.get(i)
						.getDescription());
				json.put(ChallengesTable.ANSWER, challenges.get(i).getAnswer());
				json.put(ChallengesTable.HASHTAG, challenges.get(i)
						.getHashtag());
				json.put(ChallengesTable.PHOTO_PATH, challenges.get(i)
						.getPhotoPath());
				json.put(ChallengesTable.CHALLENGE_TYPE, challenges.get(i)
						.getChallengeType().asInt());
				json.put(ChallengesTable.CHALLENGED_ID, challenges.get(i)
						.getChallengedID());
				json.put(ChallengesTable.CHALLENGER_ID, challenges.get(i)
						.getChallenger().getFbID());
				json.put(FriendsTable.NAME, challenges.get(i).getChallenger()
						.getName());
				json.put(ChallengesTable.ACCEPTED, challenges.get(i)
						.isAccepted());
				json.put(ChallengesTable.STATUS, challenges.get(i).getStatus()
						.asInt());
			} catch (JSONException e) {
				e.printStackTrace();
			}
			jsonArr.put(json);
		}

		return jsonArr.toString();
	}

	public static List<ChallengeModel> jsonStringToChallengeList(
			String jsonString) {

		List<ChallengeModel> challenges = new ArrayList<ChallengeModel>();

		JSONArray jarr = null;

		try {
			jarr = new JSONArray(jsonString);
		} catch (JSONException e) {
			e.printStackTrace();
		}

		try {
			for (int i = 0; i < jarr.length(); i++) {
				JSONObject currentItem = (JSONObject) jarr.get(i);

				int id = currentItem.getInt(ChallengesTable.ID);
				String description = null;
				if (!currentItem.isNull(ChallengesTable.DESCRIPTION)) {
					description = currentItem
							.getString(ChallengesTable.DESCRIPTION);
				}

				String answer = null;
				if (!currentItem.isNull(ChallengesTable.ANSWER)) {
					answer = currentItem.getString(ChallengesTable.ANSWER);
				}

				String hashtag = currentItem.getString(ChallengesTable.HASHTAG);

				String photoPath = null;
				if (!currentItem.isNull(ChallengesTable.PHOTO_PATH)) {
					photoPath = currentItem
							.getString(ChallengesTable.PHOTO_PATH);
				}

				int type = currentItem.getInt(ChallengesTable.CHALLENGE_TYPE);

				ChallengeType chType = null;
				if (type == 1) {
					chType = ChallengeType.TEXT;
				} else if (type == 2) {
					chType = ChallengeType.PHOTO;
				} else {
					chType = ChallengeType.TEXT;
				}

				long challengedId = currentItem
						.getLong(ChallengesTable.CHALLENGED_ID);
				long challengerId = currentItem
						.getLong(ChallengesTable.CHALLENGER_ID);

				String challengerName = null;
				if (!currentItem.isNull(FriendsTable.NAME)) {
					challengerName = currentItem.getString(FriendsTable.NAME);
				} else if (challengerId != challengedId){
					challengerName = Consts.COMPANY_NAME;
				} else {
					challengerName = Consts.ME;
				}

				FriendModel challengerFriend = new FriendModel(challengerId,
						challengerName);

				boolean isAccepted = currentItem
						.getBoolean(ChallengesTable.ACCEPTED);
				int status = currentItem.getInt(ChallengesTable.STATUS);
				ChallengeStatus chStatus = null;

				switch (status) {
				case 1:
					chStatus = ChallengeStatus.IN_DATABASE;
					break;
				case 2:
					chStatus = ChallengeStatus.IN_MY_CHALLENGES;
					break;
				case 3:
					chStatus = ChallengeStatus.PENDING_FOR_FACEBOOK;
					break;
				case 4:
					chStatus = ChallengeStatus.IN_HISTORY;
					break;
				case 5:
					chStatus = ChallengeStatus.PENDING_FOR_SERVER;
					break;
				default:
					break;
				}

				ChallengeModel ch = new ChallengeModel(id, description, answer,
						chType, photoPath, challengerFriend, challengedId,
						isAccepted, chStatus);

				ch.setHashtag(hashtag);

				challenges.add(ch);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}

		return challenges;
	}
}
